﻿using System;

namespace Batman.Cache.Attributes
{
    /// <summary>
    /// 缓存设定属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class CacherAttribute : Attribute
    {
        /// <summary>
        /// 缓存唯一Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 缓存着类型
        /// </summary>
        public CacherType CacherType { get; set; }

        /// <summary>
        /// 缓存时间，单位秒
        /// </summary>
        public int CacheSeconds { get; set; }

        /// <summary>
        /// 清除缓存的目标配合 CacherType= CacherType.清除缓存 起作用
        /// </summary>
        public string TargetId { get; set; }

        public CacherAttribute() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">缓存KEY 如果空则是被拦截方法的方法名</param>
        /// <param name="cacherType"></param>
        /// <param name="targetId">清除缓存的目标ID，配合CacherType= CacherType.清除缓存 起作用</param>
        public CacherAttribute(string id, CacherType cacherType = CacherType.Cache, string targetId = "")
        {
            Id = id;
            CacherType = cacherType;
            TargetId = targetId;
        }

        public CacherAttribute(CacherType cacherType = CacherType.Cache, string targetId = "")
        {
            CacherType = cacherType;
            TargetId = targetId;
        }
    }

    public enum CacherType
    {
        // 加上这个类型的CacherAttribute将把结果缓存
        Cache = 0,

        // 加上这个类型的CacherAttribute的方法执行结束后，将把缓存目标缓存清除
        Clean = 1
    }
}
